package com.alibaba.itu.tala.ui.view;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.Label;
import jxl.write.NumberFormats;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.eclipse.swt.widgets.Shell;

import com.alibaba.itu.tala.model.ReviewIssue;
import com.alibaba.itu.tala.util.TalaLogger;

/**
 * Export Review data to excel
 * 
 * @author tengfei.fangtf
 * 
 */
public class ReviewExportSaveDialog extends SaveDialog {

	private static TalaLogger log = TalaLogger.getLogger();

	public static final int MAX_EXPORT_NUM = 1000;// max export num

	private List<ReviewIssue> reviewIssues = new ArrayList<ReviewIssue>();//export data

	private SimpleDateFormat shortDateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
	/**
	 * 
	 * ReviewExportSaveDialog constructor
	 * 
	 * @param shell
	 *            the parent shell
	 */
	public ReviewExportSaveDialog(Shell shell) {
		super(shell);
		setText("Tala Review Export");
		setFilterExtensions(new String[] { "*.xls" });
		setFileName("tala_"+shortDateFormat.format(new Date())+".xls");
	}

	@Override
	boolean saveFile(File file) {
		WritableWorkbook workbook = null;
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			workbook = Workbook.createWorkbook(fos);
			WritableSheet ws = workbook.createSheet("Review List", 0);
			createSheetHeader(ws);
			createSheetContext(ws);
			workbook.write();
			fos.flush();
		} catch (Exception e) {
			log.error("has error when export review to excel", e);
			return false;
		} finally {
			try {
				workbook.close();
				fos.close();
			} catch (Exception e) {
				log.error("has error when close io", e);
			}
		}
		return true;
	}

	private void createSheetContext(WritableSheet ws)
			throws RowsExceededException, WriteException {
		WritableSheet rtnws = ws;
		Label label;
		int rowNum = 1;
		WritableCellFormat wf = new WritableCellFormat(NumberFormats.TEXT);
		wf.setAlignment(Alignment.LEFT);
		for (ReviewIssue reviewIssue : getReviewIssues()) {
			label = new Label(0, rowNum, reviewIssue.getProject());
			rtnws.addCell(label);
			label = new Label(1, rowNum, reviewIssue.getReviewer());
			rtnws.addCell(label);
			label = new Label(2, rowNum, reviewIssue.getAssignedTo());
			rtnws.addCell(label);
			label = new Label(3, rowNum, reviewIssue.getSummary());
			rtnws.addCell(label);
			label = new Label(4, rowNum, reviewIssue.getStatus());
			rtnws.addCell(label);
			label = new Label(5, rowNum, reviewIssue.getType());
			rtnws.addCell(label);
			label = new Label(6, rowNum, reviewIssue.getSeverity());
			rtnws.addCell(label);
			label = new Label(7, rowNum, reviewIssue.getGmtCreated().toString());
			rtnws.addCell(label);
			label = new Label(8, rowNum, reviewIssue.getDescription());
			rtnws.addCell(label);
			rowNum++;
		}

	}

	private void createSheetHeader(WritableSheet ws)
			throws RowsExceededException, WriteException {
		//set Column width
		WritableSheet rtnws = ws;
		rtnws.setColumnView(0, 20);
		rtnws.setColumnView(1, 15);
		rtnws.setColumnView(2, 15);
		rtnws.setColumnView(3, 35);
		rtnws.setColumnView(4, 15);
		rtnws.setColumnView(5, 15);
		rtnws.setColumnView(6, 15);
		rtnws.setColumnView(7, 25);
		rtnws.setColumnView(8, 15);

		// create Font
		WritableFont font = new WritableFont(WritableFont.ARIAL, 10,
				WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
				Colour.BLACK);
		WritableCellFormat f = new WritableCellFormat(font);

		Label label;
		// set title and Format
		label = new Label(0, 0, "project", f);
		rtnws.addCell(label);
		label = new Label(1, 0, "reviewer", f);
		rtnws.addCell(label);
		label = new Label(2, 0, "assigned to", f);
		rtnws.addCell(label);
		label = new Label(3, 0, "summary", f);
		rtnws.addCell(label);
		label = new Label(4, 0, "status", f);
		rtnws.addCell(label);
		label = new Label(5, 0, "type", f);
		rtnws.addCell(label);
		label = new Label(6, 0, "severity", f);
		rtnws.addCell(label);
		label = new Label(7, 0, "create time", f);
		rtnws.addCell(label);
		label = new Label(8, 0, "description", f);
		rtnws.addCell(label);
	}

	public List<ReviewIssue> getReviewIssues() {
		return reviewIssues;
	}

	public void setReviewIssues(List<ReviewIssue> reviewIssues) {
		this.reviewIssues = reviewIssues;
	}
}
